<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Admin extends CI_Controller {

    var $data = array();

    function __construct() {
        parent::__construct();

        $this->load->helper(array("url"));
        $this->load->model("Mtaikhoan");
        $this->load->model("Mtacpham");
        $this->load->model("Mtheloai");
        $this->load->model("Mtacgia");
        $this->load->model('Mnhaxuatban');
        $this->load->library("autogenid");



        // $this->load->model("Phanloaihoa");
        //  $this->load->model("Hoadon", "hoadon");
        //  $this->load->model("Sanpham", "sanpham");
        //  $this->load->model("Ct_hoadon", "ct_hoadon");
        //  $this->load->model("Khachhang", "khachhang");
    }

    public function load_page($data) {
        $maloaitk = $this->session->userdata('maloaitk');
        //admin
        if ($maloaitk == 'LTK01') {
            $this->load->view('admin/index', $data);
        } else if ($this->session->userdata('permission') == "0") {
            $data['link'] = '';
            $data['link_logout'] = base_url() . 'index.php/welcome/dangxuat';
            $data['log_out'] = 'ĐĂNG XUẤT';
            $data['text_before'] = 'TÀI KHOẢN';
            $data['text_after'] = $this->session->userdata('email');
            $data['include'] = 'member.php';
            $this->load->view('error', $data);
        } else {
            $data['link'] = base_url() . 'index.php/welcome/dangky';
            $data['link_logout'] = '';
            $data['log_out'] = '';
            $data['text_before'] = 'ĐĂNG NHẬP';
            $data['text_after'] = 'ĐĂNG KÝ';
            $data['include'] = 'login.php';
            $this->load->view('error', $data);
        }
    }

    function index() {
        $data['include'] = 'charts.php';
        $this->load_page($data);
    }

    public function sach() {
        $sach = new Mtacpham();
        $data['sachs'] = $sach->getListObject("*", "1=1");
        $theloai = new Mtheloai();
        $data['theloais'] = $theloai->getListObject("*", "1=1");
        $data['include'] = 'sach.php';
        $tacgia = new Mtacgia();
        $data['tacgias'] = $tacgia->getListObject("*", "1=1");
        $nxb = new Mnhaxuatban();
        $data['nxbs'] = $nxb->getListObject("*", "1=1");
        $this->load_page($data);
    }

    public function GetSach() {
        $matp = $_POST['matp'];
        $tacpham = new Mtacpham();
        $tacpham->getObject("matp= '" . $matp . "'");
        $arr = array(
            "matp" => $tacpham->get_matp(),
            "caption" => $tacpham->get_caption(),
            "giaban" => $tacpham->get_giaban(),
            "giagoc" => $tacpham->get_giagoc(),
            "hinhanh" => $tacpham->get_hinhanh_img(),
            "manxb" => $tacpham->get_manxb(),
            "matg" => $tacpham->get_matg(),
            "namxb" => $tacpham->get_namxuatban(),
            "sotrang" => $tacpham->get_sotrang(),
            "tentp" => $tacpham->get_tentp(),
            "matl" => $tacpham->get_matl()
        );

        echo json_encode($arr);
    }

    public function sach_add() {
        $sach = new Mtacpham();
        $pre = 'TP';
        $beforeID = $sach->GetLastID();
        $genid = new autogenid();
        $matp = $genid->genID($pre, $beforeID);
        $tentp = $_POST['tentp'];
        //  $hinhanh = $_POST['hinhanh'];
        $matl = $_POST['matl'];
        $matg = $_POST['matg'];
        $namxuatban = $_POST['namxuatban'];
        // $manxb=$_POST['manxb'];
        $sotrang = $_POST['sotrang'];
        $caption = $_POST['caption'];
        $giaban = $_POST['giaban'];
        $giagoc = $_POST['giagoc'];
        $manxb = $_POST['manxb'];
        $fileName = $_FILES['file']['name'];
        $tmpName = $_FILES['file']['tmp_name'];
        $fileSize = $_FILES['file']['size'];
        $fileType = $_FILES['file']['type'];
        $fp = fopen($tmpName, 'r');
        $content = fread($fp, filesize($tmpName));
        fclose($fp);
        $hinhanh = $content;
        $sach->set_caption($caption);
        $sach->set_giaban($giaban);
        $sach->set_giagoc($giagoc);
        $sach->set_hinhanh($hinhanh);
        $sach->set_manxb($manxb);
        $sach->set_matg($matg);
        $sach->set_matl($matl);
        $sach->set_matp($matp);
        $sach->set_namxuatban($namxuatban);
        $sach->set_sotrang($sotrang);
        $sach->set_tentp($tentp);

      
  if( $sach->Save() ) $matp=TRUE;
        $data = array('matp' => $matp);
        echo json_encode($data);
        

    }

    public function sach_upd() {
        $sach = new Mtacpham();

        $matp = $_POST['matp'];
        $tentp = $_POST['tentp'];
        $matl = $_POST['matl'];
        $matg = $_POST['matg'];
        $namxuatban = $_POST['namxuatban'];
        // $manxb=$_POST['manxb'];
        $sotrang = $_POST['sotrang'];
        $caption = $_POST['caption'];
        $giaban = $_POST['giaban'];
        $giagoc = $_POST['giagoc'];
        $manxb = $_POST['manxb'];
        if (isset($_FILES['file']['name'])) {
            $fileName = $_FILES['file']['name'];
            $tmpName = $_FILES['file']['tmp_name'];
            $fileSize = $_FILES['file']['size'];
            $fileType = $_FILES['file']['type'];
            $fp = fopen($tmpName, 'r');
            $content = fread($fp, filesize($tmpName));
            fclose($fp);
            $hinhanh = $content;
            $sach->set_hinhanh($hinhanh);
        }
        $sach->set_caption($caption);
        $sach->set_giaban($giaban);
        $sach->set_giagoc($giagoc);

        $sach->set_manxb($manxb);
        $sach->set_matg($matg);
        $sach->set_matl($matl);
        $sach->set_matp($matp);
        $sach->set_namxuatban($namxuatban);
        $sach->set_sotrang($sotrang);
        $sach->set_tentp($tentp);

        $sach->UpdateField('matp="' . $matp . '"');
		$sach->getObject('matp="' . $matp . '"');
		ob_clean();
        $arr = array(
            "matp" => $sach->get_matp(),
            "caption" => $sach->get_caption(),
            "giaban" => $sach->get_giaban(),
            "giagoc" => $sach->get_giagoc(),
            "hinhanh" => $sach->get_hinhanh_img(),
            "manxb" => $sach->get_manxb(),
            "matg" => $sach->get_matg(),
            "namxb" => $sach->get_namxuatban(),
            "sotrang" => $sach->get_sotrang(),
            "tentp" => $sach->get_tentp(),
            "matl" => $sach->get_matl()
        );

        echo json_encode($arr);
    }
    public function sach_del(){
        $sach=new Mtacpham();
        $matp=$_POST['matp'];
        $sach->Delete('matp="'.$matp.'"');
    }

    public function theloai() {
        $theloais = new Mtheloai();
        $data['theloais'] = $theloais->getListObject("*", "1=1");
        $data['include'] = 'theloai.php';
        $this->load_page($data);
    }

    public function theloai_add() {
        $tentl = $_POST['tentl'];
        $theloai = new Mtheloai();
        $theloai->set_tentl($tentl);
        $pre = 'TL';
        $beforeID = $theloai->GetLastID();
        $genid = new autogenid();
        $matl = $genid->genID($pre, $beforeID);
        $theloai->set_matl($matl);
        $theloai->Save();
    }

    public function edittheloai() {
        $matl = $_POST['matl'];
        $theloai = new Mtheloai();
        $theloai->getObject("matl='" . $matl . "'");
        $data['theloai'] = $theloai;
        $this->load->view("admin/edittheloai", $data);
    }

    public function theloai_upd() {
        $matl = $_POST['matl'];
        $tentl = $_POST['tentl'];

        $theloai = new Mtheloai();
        $theloai->set_matl($matl);
        $theloai->set_tentl($tentl);

        $theloai->UpdateField("MATL='" . $matl . "'");
        $theloai->getObject("matl= '" . $matl . "'");
        $arr = array("matl" => $theloai->get_matl(),
            "tentl" => $theloai->get_tentl());
        echo json_encode($arr);
//        $data['theloai'] = $theloai;
//        $this->load->view("admin/updatetheloai", $data);
    }

    public function theloai_del() {
        $matl = $_POST['matl'];
        //$matl=$_GET['matl'];                
        $theloai = new Mtheloai();
        $theloai->Delete(array('MATL' => $matl));
    }

//    public function view_addtheloai() {
//        echo '<ul>';
//        echo '<li>';
//        echo '<h3>Thêm thể loại </h2>';
//        echo "<span class='required_notification'>* Trường bắt buộc</span>";
//        echo '</li>';
//        echo '<li>';
//        echo '<label for="name">Tên thể loại:</label>';
//        echo "<input type='text' id ='catename' name='tentl' required />";
//        echo "<span id='username_hint' class='form_hint' style='display:none'></span>";
//        echo "</li>";
//        echo '<li>';
//        echo "<button class='submit' type='submit'> Thêm </button>";
//        echo '</li>';
//        echo '</ul>';
//    }


    public function GetTL() {
        $matl = $_POST['matl'];
        $theloai = new Mtheloai();
        $theloai->getObject("matl= '" . $matl . "'");
//        echo"<div class='Get_tennxb'>" . $nxb->get_tennxb() . "</div>";
//        echo"<div class='Get_diachi'>" . $nxb->get_diachi() . "</div>";
        $arr = array(
            "matl" => $theloai->get_matl(),
            "tentl" => $theloai->get_tentl(),
        );

        echo json_encode($arr);
    }

    public function tacgia() {
        $tacgias = new Mtacgia();
        $data['tacgias'] = $tacgias->getListObject("*", "1=1");
        $data['include'] = 'tacgia.php';
        $this->load_page($data);
    }
    public function tacgia_add()
    {
        $tacgia = new Mtacgia();
        $beforeID = $tacgia->GetLastID();
        $tentg=$_POST['tentg'];
        $this->load->library("autogenid");
        $genid = new autogenid();
        $pre = "TG";
        $tieusu = htmlspecialchars_decode($_POST['tieusu']);
        $tacgia->set_matg($genid->genID($pre, $beforeID));
        $tacgia->set_tentg($tentg);
        $tacgia->set_tieusu($tieusu);
       echo $tacgia->Save();
    }
    public function tacgia_upd(){
        $tacgia = new Mtacgia();
        $matg=$_POST['matg'];
        $tentg=$_POST['tentg'];       
        $tieusu = htmlspecialchars_decode($_POST['tieusu']);
        $tacgia->set_matg($matg);
        $tacgia->set_tentg($tentg);
        $tacgia->set_tieusu($tieusu);
        $tacgia->UpdateField('matg="'.$matg.'"');
        $arr = array("matg" => $matg,
            "tentg" => $tentg,
            "tieusu" => $tieusu);
        echo json_encode($arr);
    }
    public function tacgia_del()
    {        
        $matg = $_POST['matg'];
        $tacgia = new Mtacgia();
        $return=false;
        if($tacgia->Delete('matg="'.$matg.'"'))
                $return =TRUE;
                $arr=array("rs"=>$return);
                echo json_encode($arr);
    }
    public function GetTG() {
        $matg = $_POST['matg'];
        $tacgia = new Mtacgia();
        $tacgia->getObject("matg= '" . $matg . "'");
        $arr = array(
            "tentg" => $tacgia->get_tentg(),
            "tieusu" => $tacgia->get_tieusu(),
        );
        echo json_encode($arr);
    }

    public function nxb() {

        $nxbs = new Mnhaxuatban();
        $data['nxbs'] = $nxbs->getListObject("*", "1=1");
        $data['include'] = 'nxb.php';
        $this->load_page($data);
    }

    public function NXB_upd() {
        $manxb = $_POST['manxb'];
        $tennxb = $_POST['tennxb'];
        $diachi = $_POST['diachi'];
        $nxb = new Mnhaxuatban();
        $nxb->set_manxb($manxb);
        $nxb->set_tennxb($tennxb);
        $nxb->set_diachi($diachi);
        $nxb->UpdateField("MANXB='" . $manxb . "'");
        $nxb->getObject("MANXB= '" . $manxb . "'");
        $arr = array('manxb' => $nxb->get_manxb(),
            'tennxb' => $nxb->get_tennxb());
        echo json_encode($arr);
    }

    public function nxb_add() {
        $tennxb = $_POST['tennxb'];
        $nxb = new Mnhaxuatban();
        $beforeID = $nxb->GetLastID();
        $this->load->library("autogenid");
        $genid = new autogenid();
        $pre = "NXB";
        $diachi = htmlspecialchars_decode($_POST['diachi']);
        // var_dump($diachi);
        $nxb->set_diachi($diachi);
        $manxb = $genid->genID($pre, $beforeID);
        $nxb->set_manxb($manxb);
        $nxb->set_tennxb($tennxb);
        echo $nxb->Save();
    }

    public function GetNXB() {
        $manxb = $_POST['manxb'];
        $nxb = new Mnhaxuatban();
        $nxb->getObject("manxb= '" . $manxb . "'");
//        echo"<div class='Get_tennxb'>" . $nxb->get_tennxb() . "</div>";
//        echo"<div class='Get_diachi'>" . $nxb->get_diachi() . "</div>";
        $arr = array(
            "tennxb" => $nxb->get_tennxb(),
            "diachi" => $nxb->get_diachi(),
        );

        echo json_encode($arr);
    }

    public function deteleNXB() {
        $manxb = $_POST['manxb'];
        $nxb = new Mnhaxuatban();
        echo $nxb->Delete("MANXB='" . $manxb . "'");
    }

    public function editsach() {
        $matp = $_POST['matp'];
        $phanloai = new Phanloaihoa();
        $phanloai->getObject("id= '" . $id_cate . "'");
        echo '<ul>';
        echo '<li>';
        echo '<h2>Chỉnh sửa phân loại</h2>';
        echo "<span class='required_notification'>* Trường bắt buộc</span>";
        echo '</li>';
        echo '<li>';
        echo '<label for="name">Phân loại ID:</label>';
        echo "<input type='text' id ='cateid' name='cateid' value='" . $phanloai->get_id() . "' disabled />";
        echo "</li>";
        echo '<li>';
        echo '<label for="name">Tên phân loại:</label>';
        echo "<input type='text' id ='catename' name='catename' value='" . $phanloai->get_tenloaihoa() . "' required />";
        echo "<span id='username_hint' class='form_hint' style='display:none'></span>";
        echo "</li>";

        echo '<li>';
        echo "<button class='submit' type='submit'>Cập nhật</button>";
        echo '</li>';
        echo '</ul>';
    }

}

?>
